package com.clkj.aimin.service.notice;

import com.clkj.aimin.dao.notice.NoticeMapper;
import com.clkj.aimin.dao.topic.TopicMapper;
import com.clkj.aimin.pojo.notice.Notice;
import com.clkj.aimin.pojo.topic.Topic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

/**
 * @author yawang
 * @create 2024/06/12 09:19:43
 **/
@Service
public class NoticeServiceImpl implements NoticeService{

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    TopicMapper topicMapper;
    @Override
    public void addNotice(Notice notice) {
        noticeMapper.insertSelective(notice);
    }

    @Override
    public List<Notice> listNotice() {
        Example example = new Example(Notice.class);
        example.orderBy("noticeId").desc();
        return noticeMapper.selectByExample(example);
    }

    @Override
    public void updateNotice(Notice notice) {
        noticeMapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public void deleteNotice(Integer noticeId) {
        noticeMapper.deleteByPrimaryKey(noticeId);
    }

    @Override
    public void updateNoticeStatus(Integer status, String ids) {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("noticeId", Arrays.asList(ids.split(",")));
        Notice notice = new Notice();
        notice.setNoticeStatus(status);
        noticeMapper.updateByExampleSelective(notice, example);
    }

    @Override
    public PageInfo listTopic(Integer page, Integer size, Integer doctorId, String topicName, Integer topicType) {
        PageHelper.startPage(page,size);
        Example example = new Example(Topic.class);
        Example.Criteria criteria = example.createCriteria();
        if(doctorId!=0){
            criteria.andEqualTo("doctorId",doctorId);
        }
        if(topicType!=-1){
            criteria.andEqualTo("topicType",topicType);
        }
        if(topicName.length()!=0||topicName!=null){
            criteria.andCondition(" topic_title like '%"+topicName+"%' ");
        }
        example.orderBy("topicId").desc();
        return new PageInfo(topicMapper.selectByExample(example));
    }
}
